//
//  main.cpp
//  opencv_project
//
//  Created by 黄琛 on 2018/12/26.
//  Copyright © 2018 黄琛. All rights reserved.
//

#include "opencv2/opencv.hpp"

using namespace cv;
using namespace std;

int main(void)
{
    Mat src = imread("pic6.png");
    
    //由于threshold只支持灰度图像，所以先转换
    Mat gray;
    cvtColor(src, gray, CV_BGR2GRAY);
    
    //形态学处理，去除噪声
    Mat element = getStructuringElement(MORPH_CROSS, Size(3, 3));
    morphologyEx(gray, gray, MORPH_OPEN, element);
    morphologyEx(gray, gray, MORPH_CLOSE, element);
    
    //使用大津算法全局阈值分割
    Mat bw;
//    threshold(gray, bw, 0, 255, CV_THRESH_OTSU);
    
    //使用局部阈值法分割
    adaptiveThreshold(gray, bw, 255, ADAPTIVE_THRESH_GAUSSIAN_C, THRESH_BINARY_INV, 7, 10);
    
    imshow("src", src);
    imshow("gray", gray);
    imshow("result of adaptiveThreshold", bw);
    
    waitKey(0);

    return 0;
}
